/*
 * Copyright (C) 2014 Hortator
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package emergence.net.chat;

import java.util.ArrayList;
import java.util.List;
import util.Logger;

/**
 * A class representing a chat.
 * @author Nathan Templon
 */
public class Chat {

    private static Logger logger = Logger.getDefaultLogger();
    
    // The chat messages, in the order that they were created
    private final List<ChatMessage> chatMessages;

    /**
     * Default constructor
     */
    public Chat() {
        this.chatMessages = new ArrayList<>();
    }

    /**
     * Appends a message to the end of the chat.
     * @param message the message to be added to the chat
     */
    public void addMessage(ChatMessage message) {
        chatMessages.add(message);
    }

    public static class ChatMessage {

        private final String message;
        private final String sender;

        public static final String DELIM = ":&&:";

        public ChatMessage(String sender, String message) {
            this.message = message;
            this.sender = sender;
        }

        public ChatMessage(String transmit) {
            String[] data = transmit.split(DELIM);
            this.sender = data[0];
            this.message = data[1];
        }

        public String getMessage() {
            return message;
        }

        public String getSender() {
            return sender;
        }

        @Override
        public String toString() {
            return sender + ": " + message;
        }

        public String transmitString() {
            return sender + DELIM + message;
        }
    }

}
